package com.app.twth.ui.b2cpick.category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemB2cpickCategoryLpnListBinding;
import com.app.twth.interfaces.MyStringOnClickListener;
import com.app.twth.ui.b2cpick.B2CPickDetails;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class B2CPickCategoryLpnAdapter extends RecyclerView.Adapter<B2CPickCategoryLpnAdapter.MyViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private MyStringOnClickListener lpnListener;
    private List<B2CPickDetails.IbLpnListDTO> data = new ArrayList<>();

    public B2CPickCategoryLpnAdapter(Context mContext, List<B2CPickDetails.IbLpnListDTO> data, MyStringOnClickListener lpnListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.lpnListener = lpnListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemB2cpickCategoryLpnListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemB2cpickCategoryLpnListBinding itemBinding;

        public MyViewHolder(ItemB2cpickCategoryLpnListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            B2CPickDetails.IbLpnListDTO lpn = data.get(position);

            itemBinding.tvItem.setOnClickListener(v -> {
                if (lpnListener == null) {
                    return;
                }
                lpnListener.OnClick(lpn.getLpn());
            });
            if (Utils.isEmpty(lpn.getLpn())) {
                itemBinding.tvItem.setText("--(" + lpn.getSkuQty() + ")");
            } else {
                itemBinding.tvItem.setText(lpn.getLpn() + "(" + lpn.getSkuQty() + ")");
            }
        }
    }

}
